package dk.dashboardFP.panels.displayPanel.tools {
	import dk.dashboardFP.elements.IElement;
	import dk.dashboardFP.elements.button.ButtonElement;
	import dk.dashboardFP.elements.buttonGroup.ButtonGroup;
	import dk.dashboardFP.interactionElementFramwork.buttons.ButtonEvent;

	import flash.display.Sprite;

	/**
	 * @author josef
	 */
	public class ToggleButtonGroup extends ButtonGroup {
		private var _sprButtons : Sprite;
		private var _nCountX : int;
		private var _o_oButtonElements : Array;
		private var _doCurrentlySelectedBE : ButtonElement;

		public function ToggleButtonGroup(sLabel : String = "") {
			super(sLabel);
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override protected function setupElements() : void {
			super.setupElements();
			
			//:::::::
			// containers 
			_o_oButtonElements = [];
			
			_sprButtons = new Sprite();
			addChild(_sprButtons);
			
			_sprButtons.y = 13;
			
			//:::::::
			// counts
			_nCountX = 3; 
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// selection
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function selectById(oID:Object) : void {
			if (oID) {
				selectByBE(ButtonElement(_o_oButtonElements[oID]));
			}
		}

		private function selectByBE(doButtonElement : ButtonElement) : void {
			
			//:::::::
			// return if alrady selected 
			if (doButtonElement==_doCurrentlySelectedBE) return;
			
			//:::::::
			// delselct current 
			if (_doCurrentlySelectedBE) {
				_doCurrentlySelectedBE.oButton.setDeSelected();
			}
			
			//:::::::
			// set new selection 
			doButtonElement.oButton.setSelected();
			_doCurrentlySelectedBE = doButtonElement;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// add elements
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override public function addElement(oElement : IElement) : IElement {

			super.addElement(oElement);
			var doButtonElement : ButtonElement = oElement as ButtonElement;
			
			_o_oButtonElements[doButtonElement.oID] = doButtonElement;
			
			//:::::::
			// place 
			_sprButtons.addChild(doButtonElement);
			doButtonElement.x = _nCountX;
			
			_nCountX += doButtonElement.width+5;;
			
			//:::::::
			// register
			doButtonElement.oButton.addEventListener(ButtonEvent.CLICKED, onButtonClicked); 
			
			//:::::::
			// return 
			return doButtonElement;
		}

		override public function get nBottomLine() : Number {
			return y+35;
		}

		protected function onButtonClicked(event : ButtonEvent) : void {
		}

		protected function enableGroup() : void {
			for each (var doBE : ButtonElement in _o_oButtonElements) {
				doBE.oButton.enable();
			}
		}

		protected function disableGroup() : void {
			_doCurrentlySelectedBE=null;
			for each (var doBE : ButtonElement in _o_oButtonElements) {
				doBE.oButton.setDeSelected();
				doBE.oButton.disable();
			}
		}
	}
}
